import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/hj_model/splash/ads_model.dart';
import 'package:xvideos/hj_model/video_model.dart';

import 'acg/comic_chapters_model.dart';
import 'actress_info.dart';
import 'media_content.dart';

class CartoonMediaInfo {
  String? author; //作者
  int? countBrowse; //浏览数
  int? countCollect; //收藏数
  int? countDisLike; //不喜欢数
  int? countComment; //评论数
  int? countLike; //喜欢数
  int? countPurchases; //购买数
  int? countView; //展现数
  String? createdAt; //文档创建时间
  int? freeEpisode; //免费集数
  bool? hasFollow; //是否关注
  String? horizontalCover; //横版封面
  String? id; //文档id
  int? kind; //种类(1、哩番,2、3D,3、同人动画,4、单行本,5、连载漫画,6、韩漫 ）
  String? mId; //模块ID
  VidStatus? mediaStatus; //媒体状态
  String? mediaType; //媒体类型 "video":动漫,"image":漫画 "text":小说
  String? moduleName; //模块名称
  int? permission; // 0:会员 1:金币购买 2:免费
  bool? permissionIconHide; //售卖类型标识隐藏
  int? price; //价格(整部购买的类型 小说 cos 写真)
  int? discountPrice;
  int? contentsPrice; //所有子集的价格
  String? sId; //专题id
  String? sectionName; //专题名称
  int? sectionSort; //专题排序
  int? sortCode; //排序
  int? status; //状态

  String? summary; //简介
  List<TagsBean>? tagDetails; //标签对象
  String? title; //标题
  int? totalEpisode; //总集数
  int? currentEpisode; //当前集数
  int? updateStatus; //更新状态 0 默认 1、更新中 2、已完结
  String? updateTime; //文档更新时间
  String? verticalCover; //竖版封面
  int? mediaSubType; //子类型 ，暂时只有小说使用 0-默认文本小说 1-有声小说
  List<MediaContent>? contents;
  List<ActressInfo>? allActress;
  int? choiceSort;
  int? countPurchasesRate;
  int? direction;
  int? freeTime;
  bool? isDelete;
  String? updatedAct;
  AdsInfoModel? randomAdsInfo; // 随机广告

  ActressInfo? actress;
  List<ComicChapterInfo>? episodeList; // 通过接口获取，外部赋值（改版了，不是同一个接口返回）
  int? episodeCurPage; //
  int episodePageSize = 40; //
  int isUpSort = 0; //0-正序 1-倒序
  bool haxNextEpisode = true;
  int? episodeNumber; //本地添加字段。用于判断，跳转到播放详情的index
  bool isSelected = false;
  String? heroTag; //本地字段，用于传递hero动画tag 随机数

  String? get coverH => horizontalCover?.isNotEmpty == true ? horizontalCover : verticalCover;

  String? get coverV => verticalCover?.isNotEmpty == true ? verticalCover : horizontalCover;

  String get unit => mediaType == 'image' ? '话' : '章';
  //更新状态
  String get getUpdateString => updateStatus == 1 ? '连载中' : '已完结';

  String get detailUpdateString => updateStatus == 1 ? '更新至$currentEpisode$unit' : '已完结 共$totalEpisode$unit';

  //更新状态
  String get getDetailUpdateString {
    if (updateStatus == 2 || updateStatus == 0) {
      return '已完结';
    } else {
      return '连载中';
    }
  }

  Color get getDetailUpdateColor {
    if (updateStatus == 2 || updateStatus == 0) {
      return Color(0xff989898);
    } else {
      return AppColors.actionRed;
    }
  }

  //是否是有声小说
  bool get isAudiobooks => mediaType == 'text' && mediaSubType == 1;

  //集数状态描述
  String get episodeNumberStatus {
    if (updateStatus == 1) {
      if (currentEpisode == 0) {
        return '共$totalEpisode$unit';
      } else {
        return '更新$currentEpisode$unit';
      }
    } else {
      return '共$totalEpisode$unit';
    }
  }

  //评论类型，传参数类型
  String get commentType => 'cartoon';

  CartoonMediaInfo(
      {this.contents,
      this.choiceSort,
      this.countBrowse,
      this.countCollect,
      this.countComment,
      this.countLike,
      this.countPurchases,
      this.countPurchasesRate,
      this.countView,
      this.createdAt,
      this.direction,
      this.freeTime,
      this.horizontalCover,
      this.id,
      this.isDelete,
      this.kind,
      this.mId,
      this.mediaType,
      this.moduleName,
      this.permission,
      this.permissionIconHide,
      this.price,
      this.sId,
      this.discountPrice,
      this.sectionName,
      this.sectionSort,
      this.sortCode,
      this.status,
      this.summary,
      this.tagDetails,
      this.title,
      this.totalEpisode,
      this.currentEpisode,
      this.updateStatus,
      this.updateTime,
      this.updatedAct,
      this.verticalCover,
      this.author,
      this.freeEpisode,
      this.contentsPrice,
      this.mediaSubType,
      this.actress});

  CartoonMediaInfo.fromJson(dynamic json) {
    freeEpisode = json['freeEpisode'];
    mediaSubType = json['mediaSubType'];
    author = json['author'];

    choiceSort = json['choiceSort'];
    totalEpisode = json['totalEpisode'];
    currentEpisode = json['currentEpisode'];
    countBrowse = json['countBrowse'];
    countCollect = json['countCollect'];
    countComment = json['countComment'];
    countLike = json['countLike'];
    countDisLike = json['countDisLike'];
    countPurchases = json['countPurchases'];
    countPurchasesRate = json['countPurchasesRate'];
    discountPrice = json['discountPrice'];
    countView = json['countView'];
    createdAt = json['createdAt'];
    direction = json['direction'];
    freeTime = json['freeTime'];
    horizontalCover = json['horizontalCover'];
    id = json['id'];
    isDelete = json['isDelete'];
    kind = json['kind'];
    mId = json['mId'];
    mediaType = json['mediaType'];
    moduleName = json['moduleName'];

    permission = json['permission'];
    permissionIconHide = json['permissionIconHide'];
    price = json['price'];
    sId = json['sId'];
    sectionName = json['sectionName'];
    sectionSort = json['sectionSort'];

    sortCode = json['sortCode'];
    status = json['status'];
    summary = json['summary'];
    actress = json["actress"] == null ? null : ActressInfo.fromJson(json["actress"]);
    allActress = json["actresses"] == null ? [] : List<ActressInfo>.from(json["actresses"]!.map((x) => ActressInfo.fromJson(x)));
    hasFollow = json['hasFollow'];
    if (json['tagDetails'] != null) {
      tagDetails = [];
      json['tagDetails'].forEach((v) {
        tagDetails?.add(TagsBean.fromMap(v));
      });
    }
    if (json['contents'] != null) {
      contents = [];
      json['contents'].forEach((v) {
        contents?.add(MediaContent.fromJson(v));
      });
    }

    if (json['title'] == null) {
      title = json['name'];
    } else {
      title = json['title'];
    }
    updateStatus = json['updateStatus'];
    updateTime = json['updateTime'];
    updatedAct = json['updatedAct'];
    verticalCover = json['verticalCover'];
    mediaStatus = VidStatus.fromMap(json['mediaStatus']);
    contentsPrice = json['contentsPrice'];
  }

  ///videoModel是否随机广告
  bool isRandomAd() {
    return randomAdsInfo != null;
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['mediaSubType'] = mediaSubType;
    map['choiceSort'] = choiceSort;
    map['countBrowse'] = countBrowse;
    map['countCollect'] = countCollect;
    map['countComment'] = countComment;
    map['countLike'] = countLike;
    map['countDisLike'] = countDisLike;
    map['countPurchases'] = countPurchases;
    map['countPurchasesRate'] = countPurchasesRate;
    map['countView'] = countView;
    map['createdAt'] = createdAt;
    map['direction'] = direction;
    map['freeTime'] = freeTime;
    map['horizontalCover'] = horizontalCover;
    map['id'] = id;
    map['isDelete'] = isDelete;
    map['kind'] = kind;
    map['mId'] = mId;
    map['mediaType'] = mediaType;
    map['moduleName'] = moduleName;

    map['permission'] = permission;
    map['permissionIconHide'] = permissionIconHide;
    map['price'] = price;
    map['sId'] = sId;
    map['sectionName'] = sectionName;
    map['sectionSort'] = sectionSort;

    map['sortCode'] = sortCode;
    map['status'] = status;
    map['summary'] = summary;
    if (tagDetails != null) {
      map['tagDetails'] = tagDetails?.map((v) => v.toJson()).toList();
    }
    if (contents != null) {
      map['contents'] = contents?.map((v) => v.toJson()).toList();
    }
    map['hasFollow'] = hasFollow;
    map['title'] = title;
    map['totalEpisode'] = totalEpisode;
    map['currentEpisode'] = currentEpisode;
    map['updateTime'] = updateTime;
    map['updateStatus'] = updateStatus;
    map['updatedAct'] = updatedAct;
    map['verticalCover'] = verticalCover;
    map['mediaStatus'] = mediaStatus;
    return map;
  }
}
